<template>
	<view class="home-container">
		<view class="intro">广播名称：com.android.server.scannerservice.broadcast</view>
		<view class="intro">广播键值：scannerdata</view>

		<view class="intro">主动开始扫描广播：com.scan.onStartScan</view>
		<view class="intro">主动结束扫描广播：com.scan.onEndScan</view>

		<text class="intro">详见：</text>
		<view class="href">
			<uni-link :href="href" text="Seuic东集-CRUISE Ge 工业级智能产品说明pdf"></uni-link>
		</view>
		<br />

		<view class="btn-list">
			<u-button type="warning" text="震动" style="width: 40%;" @click="vibrate"></u-button>
			<u-button type="warning" text="蜂鸣" style="width: 40%;" @click="beep"></u-button>
		</view>

		<view class="btn-list">
			<u-button type="primary" text="开始扫码" style="width: 40%;" @click="openScan"></u-button>
			<u-button type="primary" text="结束扫码" style="width: 40%;" @click="closeScan"></u-button>
		</view>

		<view class="result-container">
			<u--textarea v-model="result" height="200" maxlength="-1" placeholder="扫描结果"></u--textarea>
		</view>
	</view>
</template>

<script>
	import scanCode from '@/components/scanCode/scanCode.vue'; // 扫码接收
	import { broadcastScan } from '@/utils/scanCode.js';
	export default {
		data() {
			return {
				href: 'https://code.dt-insight.com/pdf?url=https://obs-dj-gym.obs.cn-east-3.myhuaweicloud.com/pdf/202205/1529643516374945792.pdf',
				result: '',
			}
		},
		methods: {
			// 自定义扫描的结果回调函数
			getScanCode(code) {
				let parseData = code.trim();
				console.log('自定义扫描的结果回调函数:', parseData);
				this.result = parseData; // + '\n' + this.result;
			},
			// 设备振动
			vibrate() {
				plus.device.vibrate(2000);
			},
			// 设备蜂鸣
			beep() {
				plus.device.beep(3);
			},
			// 主动触发扫描
			openScan() {
				console.log('主动触发扫描');
				broadcastScan.triggerScan();
			},
			// 主动关闭扫描
			closeScan() {
				console.log('主动关闭扫描');
				broadcastScan.closeScan();
			},
		},
		// onLoad 是在页面加载时触发，用于页面的初始化操作，只执行一次
		onLoad() {
			// init传入一个回调函数做参数，在扫描时候会将数据传入此方法里，然后处理此方法的自定义业务逻辑
			broadcastScan.init(this.getScanCode);

			// #ifdef APP-PLUS
			// // 监听键按下事件
			// plus.key.addEventListener("keydown", function(e) {
			// 	console.log("keydown: " + e.keyCode);
			// }, false);
			// // 监听键松开事件
			// plus.key.addEventListener("keyup", function(e) {
			// 	console.log("keyup: " + e.keyCode);
			// }, false);
			// #endif
		},
		onUnload() {
			broadcastScan.stop();
		},
		// 在页面展示时触发，用于处理页面显示相关的逻辑，可能会多次执行，包括页面首次展示和从后台切换到前台时
		onShow() {
			broadcastScan.start();
		},
		onHide() {
			broadcastScan.stop();
		},
	}
</script>

<style lang="scss">
	.home-container {
		display: flex;
		flex-direction: column;
		padding: 20px;
		font-size: 14px;
		line-height: 24px;

		.btn-list {
			display: flex;
			flex-direction: row;
		}

		.result-container {
			margin-top: 10rpx;
		}
	}
</style>